<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>信息修改</title>
    <link rel="stylesheet" href="../views/static/common/layui/css/layui.css">
    <link rel="stylesheet" href="../views/static/admin/css/style.css">
    <script src="../views/static/common/layui/layui.js"></script>
    <script src="../views/static/common/jquery-3.3.1.min.js"></script>
    <script src="../views/static/common/vue.min.js"></script>

    <script language="javascript">
        function sumbit_sure() {
            var gnl = confirm("是否确定修改?");
            if (gnl == true) {
                return true;
            } else {
                return false;
            }
        }

        function loadData() {
            // alert("加载数据");
            $.getJSON("/BooksServlet.do", {"action": "bookHome","getHomeId":${requestScope.bookInfo.home_id}}, function (data) {
                // alert("获取数据成功");
                // alert(JSON.stringify(data));
                let vipList = data.vipList;
                let homeList = data.homeList;
                let vipSelect = document.getElementById("vip");
                vipSelect.innerHTML = ""; // 清空现有选项
                vipList.forEach(function (item) {
                    // alert(item.name);
                    let option = document.createElement("option");
                    option.value = item.id;
                    option.textContent = item.name;
                    if (item.id === ${requestScope.bookInfo.vip_id}) {
                        option.selected = true; // 选中当前住客
                    }
                    vipSelect.appendChild(option);
                });

                let homeSelect = document.getElementById("home");
                // alert(homeList);
                homeSelect.innerHTML = ""; // 清空现有选项
                homeList.forEach(function (item) {
                    // alert(item.name);
                    let option = document.createElement("option");
                    option.value = item.id;
                    option.textContent = item.num+"-"+item.h_type;
                    if (item.id === ${requestScope.bookInfo.home_id}) {
                        option.selected = true;
                    }
                    homeSelect.appendChild(option);
                });
            });
        }

        function updateBook(){
            $.post("/BooksServlet.do",{
                "action":"update",
                "id":$("[name='id']").val(),
                "vip_id":$("[name='name']").val(),
                "enterTime":$("[name='enterTime']").val(),
                "exitTime":$("[name='exitTime']").val(),
                "home_id":$("[name='num']").val(),
                "lastHomeId":${requestScope.bookInfo.home_id}
            },function (data){
                if(data){
                    layer.msg("修改成功", {icon: 1, time: 1000}, function () {
                        window.location.href = "/views/guests_list.jsp";
                    });
                }else{
                    layer.msg("修改失败", {icon: 2, time: 1000}, function () {
                        window.location.href = "/views/guests_list.jsp";
                    });
                }
            }, "json");
        }

        function ckTime(){
            let enterTime = $("[name='enterTime']").val();
            let exitTime = $("[name='exitTime']").val();
            if (new Date(exitTime) <= new Date(enterTime)) {
                $("[name='exitTime']").next("span").text("退房时间不能早于入住时间");
                $("[name='exitTime']").val("");
            }else {
                $("[name='exitTime']").next("span").text("");
            }
        }
    </script>

</head>
<body onload="loadData()">
<div>
    <!--顶栏-->
    <jsp:include page="../top.jsp"/>

    <div class="main" id="app">
        <!--左栏-->
        <div class="left">
            <ul class="cl">
                <!--顶级分类-->
                <li v-for="vo,index in menu" :class="{hidden:vo.hidden}">
                    <a href="javascript:;" :class="{active:vo.active}" @click="onActive(index)">
                        <i class="layui-icon" v-html="vo.icon"></i>
                        <span v-text="vo.name"></span>
                        <i class="layui-icon arrow" v-show="vo.url.length==0">&#xe61a;</i> <i v-show="vo.active"
                                                                                              class="layui-icon active">&#xe623;</i>
                    </a>
                    <!--子级分类-->
                    <div v-for="vo2,index2 in vo.list">
                        <a href="javascript:;" :class="{active:vo2.active}" @click="onActive(index,index2)"
                           v-text="vo2.name"></a>
                        <i v-show="vo2.active" class="layui-icon active">&#xe623;</i>
                    </div>
                </li>
            </ul>
        </div>
        <!--右侧-->
        <div class="right">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>信息修改</legend>
            </fieldset>


            <%--            <form class="layui-form " action="${pageContext.request.contextPath}/guests/update2" onsubmit="return sumbit_sure()" >--%>

            <input type="hidden" name="id" value="${requestScope.bookInfo.id}"/>

            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <select name="name" class="layui-input" autocomplete="off" required id="vip">
                        <option value=""></option>
                        <option value="张三">张三</option>
                        <option value="李四">李四</option>
                        <option value="王五">王五</option>
                    </select>
                </div>
            </div>



            <div class="layui-form-item">
                <label class="layui-form-label">入住时间</label>
                <div class="layui-input-block">
                    <input type="date" name="enterTime" required  autocomplete="off" class="layui-input" value="${requestScope.bookInfo.enterTime.split(" ")[0]}" onclick="this.showPicker()" onchange="ckTime()">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">退房时间</label>
                <div class="layui-input-block">
                    <input type="date" name="exitTime" required  autocomplete="off" class="layui-input" value="${requestScope.bookInfo.exitTime.split(" ")[0]}" onclick="this.showPicker()" onchange="ckTime()">
                    <span style="color: red"></span>
                </div>
            </div>



            <div class="layui-form-item">
                <label class="layui-form-label">房间号</label>
                <div class="layui-input-block">
                    <select name="num" class="layui-input" autocomplete="off" required id="home">
                        <option value=""></option>
                        <option value="101">101</option>
                        <option value="102">102</option>
                        <option value="103">103</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="button" class="layui-btn" value="确认修改" onclick="updateBook()"/>
                    <input type="button" class="layui-btn layui-btn-danger" value="取消修改"
                           onclick="javascript:history.go(-1);"/>
                    <input name="id" value="${g.id}" type="hidden"/>
                </div>
            </div>
            <%--            </form>--%>

        </div>
    </div>
</div>
<script src="../views/static/admin/js/config.js"></script>
<script src="../views/static/admin/js/script.js"></script>
</body>
</html>
